﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OrderSystem;

namespace WindowsFormsOrder
{
    public partial class Form2 : Form
    {
        public bool isADD = true;
        public OrderSystem.OrderService service=new OrderSystem.OrderService();
        public int modifiedIndex = -1;
        private Form1 father;
        public Form2(OrderSystem.OrderService service)
        {
            this.service = service;
            InitializeComponent();
        }
        public Form2(Form1 father)
        {
            this.father = father;
            this.service = father.service;
            InitializeComponent();
        }
        

        private void 确定_Click(object sender, EventArgs e)
        {
            if (isADD)
            {
                if(service.addOrder(new Order(int.Parse(textBox1.Text), new OrderDetails(new Good(textBox2.Text), new Customer(textBox3.Text), double.Parse(textBox4.Text)))))
                {
                    string ttmp = father.textBox1.Text;
                    father.textBox1.Text = "";
                    father.button6_Click(this, new System.EventArgs());
                    father.textBox1.Text = ttmp;
                    father.button6_Click(this, new System.EventArgs());
                    father.textBox1.Text = "添加成功";
                }
                else { father.textBox1.Text = "添加失败"; }
                this.Close();
            }
            else
            {
                
                if (service.modifyOrder(modifiedIndex, new OrderDetails(new Good(textBox2.Text), new Customer(textBox3.Text), double.Parse(textBox4.Text))))
                {
                    string ttmp = father.textBox1.Text;
                    father.textBox1.Text = "";
                    father.button6_Click(this, new System.EventArgs());
                    father.textBox1.Text = ttmp;
                    father.button6_Click(this, new System.EventArgs());
                    father.textBox1.Text = "修改成功";
                }
                else { father.textBox1.Text = "修改失败"; }
                this.Close();
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            if (!isADD)
            {
                Order tmp = service.getOrder(modifiedIndex).First<Order>();
                textBox1.Text = tmp.num.ToString();
                textBox2.Text = tmp.details.good.name;
                textBox3.Text = tmp.details.customer.name;
                textBox4.Text = tmp.details.amount.ToString();
            }
        }
    }
}
